package processor.html;

/**
 * The HtmlTag Class.
 *
 * Created: 2-Jan-2012
 * Last Modified: 2-Jan-2012
 * @author Shimu
 */
public class HtmlTag {

    protected final String START_TAG, END_TAG;
    /**
     * The sum of the length of the START_TAG and the length of the END_TAG.
     * Useful for StringBuilder.
     */
    protected final int LENGTH;
    //protected final HtmlAttribute ATTRIBUTE;

    public HtmlTag(String tag) {
        this(tag, HtmlAttribute.DEFAULT_ATTRIBUTE);
    }

    /**
     * 
     * @param tag
     * @param attribute 
     */
    public HtmlTag(String tag, String attribute) {
        StringBuilder builder = new StringBuilder(30);
        builder.append("<");
        builder.append(tag);
        builder.append(" ");
        builder.append(attribute);
        builder.append(">");

        this.START_TAG = builder.toString();

        builder = new StringBuilder(15);
        builder.append("</");
        builder.append(tag);
        builder.append(">");

        this.END_TAG = builder.toString();

        this.LENGTH = START_TAG.length() + END_TAG.length();
    }

    /**
     * 
     * @param tag
     * @param attribute 
     */
    public HtmlTag(String tag, HtmlAttribute attribute) {
        this(tag, attribute.toString());
    }

    /**
     * Given a StringBuilder, append to the String builder the given text
     * surrounded by this HtmlTag's START_TAG and END_TAG.
     * 
     * @param text
     * @param builder 
     */
    public void enclose(String text, StringBuilder builder) {
        builder.append(START_TAG);
        builder.append(text);
        builder.append(END_TAG);
    }

    /**
     * Given a StringBuilder, append to the String builder the given Object's
     * String representation (the String returned by the Object's toString()
     * method) surrounded by this HtmlTag's START_TAG and END_TAG.
     * 
     * @param text
     * @param builder 
     */
    public void enclose(Object object, StringBuilder builder) {
        builder.append(START_TAG);
        if (object != null) {
            builder.append(object.toString());
        }
        builder.append(END_TAG);
    }
         
}
